<template>
  <div class="storeManage-container">
    <div class="storeManage-search">
      <span class="page-title">店铺列表</span>
      <el-input placeholder="请输入搜索内容" v-model="keyWords" class="input-with-select search-keyword">
        <el-select v-model="select" slot="prepend" placeholder="请选择">
          <el-option label="门店编号" value="1"></el-option>
          <el-option label="店主姓名" value="2"></el-option>
        </el-select>
      </el-input>
      <el-button type="primary" size="small" icon="el-icon-search" @click="searchStore">搜索</el-button>
      <el-button type="info" size="small" class="search-keyword-reset" plain @click="resetSearch">重置</el-button>
      <el-button type="success" size="small" icon="el-icon-plus" @click="handelAdd"
                 v-if="checkPermissionPoints(1) == 1">
        新增
      </el-button>
      <!--<el-button type="primary" size="small" icon="el-icon-edit" @click="toggleSelection(storeData)"-->
      <!--v-show="!checkBoxIcon">批量管理-->
      <!--</el-button>-->
      <!--<el-button type="info" size="small" plain @click="toggleSelection()" v-show="checkBoxIcon">取消批量管理</el-button>-->
      <!--<el-button type="danger" size="small" icon="el-icon-delete" class="button-position" v-if="checkBoxIcon"-->
      <!--@click="batchDel">删除选中-->
      <!--</el-button>-->

    </div>

    <div class="storeManage-table">
      <el-table ref="multipleTable" :data="storeData" border stripe fit v-loading="listLoading"
                :row-style="{height:'50px'}" :header-row-style="{height:'40px'}"
                @selection-change="handleSelectionChange" :row-key="getRowKeys">
        <el-table-column type="selection" :reserve-selection="true" width="40" v-if="checkBoxIcon"></el-table-column>
        <el-table-column prop="id" label="id" width="80">
          <template slot-scope="scope">
            {{scope.row.id}}
          </template>
        </el-table-column>
        <el-table-column label="店主姓名" width="120">
          <template slot-scope="scope">
            {{scope.row.userName}}
          </template>
        </el-table-column>
        <el-table-column label="店主身份证号" width="180">
          <template slot-scope="scope">
            {{scope.row.userCardNumber}}
          </template>
        </el-table-column>
        <el-table-column label="门店名称" width="150" show-overflow-tooltip>
          <template slot-scope="scope">
            {{scope.row.shopName}}
          </template>
        </el-table-column>
        <el-table-column label="门店编号" width="150">
          <template slot-scope="scope">
            {{scope.row.shopNum}}
          </template>
        </el-table-column>
        <el-table-column label="门店类型" width="150">
          <template slot-scope="scope">
            {{scope.row.shopType | changeShopType}}
          </template>
        </el-table-column>
        <el-table-column label="门店模式" width="100">
          <template slot-scope="scope">
            {{scope.row.module | changeModule}}
          </template>
        </el-table-column>
        <el-table-column label="招聘下架权" width="120">
          <template slot-scope="scope">
            {{scope.row.authority | changeAuthority}}
          </template>
        </el-table-column>
        <el-table-column label="是否隐藏" width="100">
          <template slot-scope="scope">
            <span>{{scope.row.delStatus | changeDelStatus}}</span>
          </template>
        </el-table-column>
        <el-table-column label="生成时间" width="160">
          <template slot-scope="scope">
            <span v-if="scope.row.time">{{scope.row.time | formatTime}}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" min-width="200" fixed="right">
          <template slot-scope="{row}">
            <el-dropdown size="mini" :split-button="true" type="primary" @click="handleLook(row)"
                         v-if="checkPermissionPoints(3) == 1">查看
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="handleEdit(row)" v-if="checkPermissionPoints(4) == 1">编辑
                </el-dropdown-item>
                <el-dropdown-item @click.native="handleDelete(row)" v-if="checkPermissionPoints(2) == 1">删除
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :type="1" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                  @pagination="getStoreList"/>
    </div>

    <!--查看-->
    <el-dialog title="详细信息" :visible.sync="dialogLookVisible" width="960px" class="storeManage-dialog look-dialog">
      <el-form label-width="140px" size="mini">
        <el-form-item label="店主姓名：">
          <span>{{store.userName}}</span>
        </el-form-item>
        <el-form-item label="门店名称：">
          <span>{{store.shopName}}</span>
        </el-form-item>
        <el-form-item label="门店编号：">
          <span>{{store.shopNum}}</span>
        </el-form-item>
        <el-form-item label="门店类型：">
          <span>{{store.shopType | changeShopType}}</span>
        </el-form-item>
        <el-form-item label="门店模式：">
          <span>{{store.module | changeModule}}</span>
        </el-form-item>
        <el-form-item label="招聘下架权：">
          <span>{{store.authority | changeAuthority}}</span>
        </el-form-item>
        <el-form-item label="是否隐藏：">
          <span>{{store.delStatus | changeDelStatus}}</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" style="width: 96px" @click="dialogLookVisible = false">取消</el-button>
      </div>
    </el-dialog>

    <!--新增/编辑-->
    <el-dialog :title="textMap[dialogStatus]" :lock-scroll="true" :visible.sync="dialogEditVisible" width="960px"
               class="storeManage-dialog" @close="cancelStore">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="auto" size="mini">
        <el-form-item label="店主姓名：" prop="userName">
          <el-input v-model="ruleForm.userName" style="width: 640px;" placeholder="请选择店主姓名" :disabled="true"
                    class="input-with-select" @click.native="openStoreVisible">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="门店名称：" prop="shopName">
          <el-input style="width: 640px;" placeholder="请输入门店名称" :clearable="true"
                    v-model="ruleForm.shopName"></el-input>
        </el-form-item>
        <el-form-item label="门店编号：" prop="shopNum">
          <el-input style="width: 640px;" placeholder="请输入门店编号" :clearable="true" v-model="ruleForm.shopNum"></el-input>
        </el-form-item>
        <el-form-item label="门店类型：" prop="shopType">
          <el-radio-group v-model="ruleForm.shopType">
            <el-radio :label=0>直营店</el-radio>
            <el-radio :label=1>加盟店</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="门店模式：" prop="module">
          <el-radio-group v-model="ruleForm.module">
            <el-radio :label=1>模式一</el-radio>
            <el-radio :label=2>模式二</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="招聘下架权:" prop="authority">
          <el-radio-group v-model="ruleForm.authority">
            <el-radio :label=0>有</el-radio>
            <el-radio :label=1>无</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否隐藏：" prop="delStatus">
          <el-radio-group v-model="ruleForm.delStatus">
            <el-radio :label=0>显示</el-radio>
            <el-radio :label=1>隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" type="success" style="width: 96px" @click="saveStore" :loading="sendLoad">保存</el-button>
        <el-button size="small" style="width: 96px" @click="cancelStore">取消</el-button>
      </div>
    </el-dialog>

    <store-user-bank :storeView="storeView" @closeStoreVisible="closeStoreVisible"
                     @selectStoreVisible="selectStoreVisible"></store-user-bank>
  </div>
</template>

<script>
  import Pagination from '@/components/Pagination';
  import {mapGetters} from 'vuex';
  import {permissionPoints} from '@/utils/permission';
  import {formatDate} from "@/utils/index";
  import storeUserBank from "@/components/common/storeUserBank";
  import {addStore, deleteStore, getInitStoreList, getStoreDetail, modifyStore} from "../../../api/shop/shop";

  export default {
    name: "storeManage",
    computed: {
      ...mapGetters([
        'points'
      ])
    },
    components: {
      Pagination,
      storeUserBank
    },
    data() {
      return {
        multipleSelection: [],
        checkBoxIcon: false,
        total: 0,
        listQuery: {
          page: 1,
          limit: 20,
          shopNum: '',
          userName: ''
        },
        storeView: false,
        keyWords: '',
        select: '1',
        listLoading: false,
        dialogType: 'edit',
        dialogLookVisible: false,
        dialogEditVisible: false,
        textMap: {
          update: '编辑',
          create: '新增'
        },
        dialogStatus: '',
        storeData: [],
        store: {},
        ruleForm: {
          userId: '',
          userName: '',
          shopNum: '',
          shopType: 0,
          authority: 1,
          delStatus: 0,
          id: '',
          module: 1,
          shopName: ''
        },
        rules: {
          userName: [
            {required: true, message: '请输入店主姓名', trigger: 'blur'}
          ],
          shopName: [
            {required: true, message: '请输入门店名称', trigger: 'blur'}
          ],
          shopNum: [
            {required: true, message: '请输入门店编号', trigger: 'blur'}
          ],
          shopType: [
            {required: true, message: '请选择门店类型', trigger: 'change'}
          ],
          module: [
            {required: true, message: '请输选择店模式', trigger: 'change'}
          ],
          authority: [
            {required: true, message: '请选择招聘下架权', trigger: 'change'}
          ],
          delStatus: [
            {required: true, message: '请选择是否隐藏', trigger: 'change'}
          ]
        },
        sendLoad: false
      }
    },
    created() {
      this.getStoreList();
    },
    methods: {
      searchStore() {//搜索
        if (this.keyWords == '' || this.keyWords == undefined) {
          return this.$message.info({message: '请输入搜索内容', duration: 1000});
        }
        this.listQuery.shopNum = '';
        this.listQuery.userName = '';
        if (this.select == 1) {
          this.listQuery.shopNum = this.keyWords;
        } else if (this.select == 2) {
          this.listQuery.userName = this.keyWords;
        }
        this.listQuery.page = 1;
        this.getStoreList();
      },
      resetSearch() {
        this.select = '1';
        this.keyWords = '';
        this.listQuery.shopNum = '';
        this.listQuery.userName = '';
        this.listQuery.page = 1;
        this.getStoreList();
      },
      getStoreList() {
        this.listLoading = true;
        getInitStoreList(this.listQuery).then((res) => {
          this.listLoading = false;
          let retObj = res;
          if (retObj.code == 0 && retObj.data != null) {
            const {items, totalRecord} = retObj.data;
            this.total = totalRecord;
            this.storeData = items;
          }
        }).catch(() => {
          this.listLoading = false;
        })
      },
      checkPermissionPoints(type) {
        let pointObj = this.points.storeManage;
        return permissionPoints(type, pointObj);
      },
      handelAdd() {
        this.dialogStatus = 'create';
        this.dialogEditVisible = true;
      },
      handleLook(row) {
        this.dialogLookVisible = true;
        getStoreDetail(row.id).then((res) => {
          let retObj = res;
          if (retObj.code == 0 && retObj.data != null) {
            this.store = retObj.data;
          }
        })
      },
      handleEdit(row) {//编辑
        this.dialogStatus = 'update';
        this.dialogEditVisible = true;
        getStoreDetail(row.id).then((res) => {
          let retObj = res;
          if (retObj.code == 0 && retObj.data != null) {
            this.ruleForm = retObj.data;
          }
        })
      },
      handleDelete(row) {
        this.$confirm('此操作将删除该店铺, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteStore(row.id).then((res) => {
            let retObj = res;
            if (retObj.code == 0 && retObj.data > 0) {
              this.$message.success({message: '删除成功', duration: 2000});
              this.getStoreList();
            } else {
              this.$message.error({message: '删除失败，请重试', duration: 2000});
            }
          })
        }).catch((err) => {
        });
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      getRowKeys(row) {
        return row.id;
      },
      toggleSelection(rows) {
        if (rows) {
          this.checkBoxIcon = !this.checkBoxIcon;
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row);
          });
        } else {
          this.checkBoxIcon = false;
          this.$refs.multipleTable.clearSelection();
        }
      },
      batchDel() {//批量
        let row = this.commonSelection();
        if (row && Object.keys(row).length != 0) {
          this.handleDelete(row);
        }
      },
      commonSelection() {
        let row = {};
        let value = this.multipleSelection;
        if (value && value.length > 0) {
          let arr = [];
          value.forEach((item) => {
            arr.push(item.id);
          });
          row.id = arr.join(",");
        }
        return row;
      },
      cancelStore() {
        this.dialogEditVisible = false;
        this.$refs.ruleForm.resetFields();
      },
      saveStore() {
        if (this.dialogStatus == 'create') {
          this.$refs.ruleForm.validate((valid) => {
            if (valid) {
              this.sendLoad = true;
              addStore(this.ruleForm).then((res) => {//新增
                let retObj = res;
                this.sendLoad = false;
                if (retObj.code == 0 && retObj.data == 1) {
                  this.$refs.ruleForm.resetFields();
                  this.dialogEditVisible = false;
                  this.$message.success({message: '新增成功', duration: 2000});
                  this.getStoreList();
                } else {
                  this.$message.error({message: '新增失败，请重试', duration: 2000});
                }
              }).catch(() => {
                this.sendLoad = false;
              })
            } else {
              return false;
            }
          });
        } else {
          this.$refs.ruleForm.validate((valid) => {
            if (valid) {
              this.sendLoad = true;
              modifyStore(this.ruleForm).then((res) => {//修改
                let retObj = res;
                this.sendLoad = false;
                if (retObj.code == 0 && retObj.data == 1) {
                  this.$refs.ruleForm.resetFields();
                  this.dialogEditVisible = false;
                  this.$message.success({message: '修改成功', duration: 2000});
                  this.getStoreList();
                } else {
                  this.$message.error({message: '修改失败，请重试', duration: 2000});
                }
              }).catch(() => {
                this.sendLoad = false;
              })
            } else {
              return false;
            }
          });
        }
      },
      openStoreVisible() {
        this.storeView = true;
      },
      closeStoreVisible() {
        this.storeView = false;
      },
      selectStoreVisible(reality) {
        const {userId, userName} = reality;
        this.ruleForm.userId = userId;
        this.ruleForm.userName = userName;
        this.closeStoreVisible();
      },
    },
    filters: {
      formatTime(time) {
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss');
      },
      changeDelStatus(type) {
        if (type == 0) {
          return '显示';
        } else if (type == 1) {
          return '隐藏';
        }
      },
      changeShopType(type) {
        if (type == 0) {
          return '直营店';
        } else if (type == 1) {
          return '加盟店';
        }
      },
      changeModule(type) {
        if (type == 1) {
          return '模式一';
        } else if (type == 2) {
          return '模式二';
        }
      },
      changeAuthority(type) {
        if (type == 0) {
          return '有权下架';
        } else if (type == 1) {
          return '无权下架';
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .storeManage-container {
    .storeManage-search {
      display: flex;
      align-items: center;
      width: 100%;
      height: 80px;
      .page-title {
        padding-left: 18px;
        min-width: 160px;
        text-align: left;
        font-size: 18px;
        font-weight: bold;
        color: rgba(61, 68, 77, 1);
      }
      .search-keyword {
        margin-right: 24px;
        width: 470px;
        > > > .el-select .el-input {
          width: 110px;
        }
        > > > .el-input-group__prepend {
          background-color: #fff;
        }
      }
      .search-keyword-reset {
        margin-right: 36px;
      }
      .excel-inout-box {
        margin-left: auto;
        margin-right: 18px;
      }
    }

    .storeManage-table {
      width: 98%;
      margin-left: 18px;
      > > > .el-table td, .el-table th {
        padding: 6px 0px;
      }
      > > > .el-table__header th {
        padding: 0;
        height: 30px;
        line-height: 30px;
      }
      > > > .el-table__fixed-right {
        height: 100% !important;
      }
      > > > .el-table__column-filter-trigger {
        line-height: 28px;
        i {
          font-size: 18px;
        }
      }
      > > > .el-table__row .cell {
        display: flex;
        align-items: center;
      }
      > > > .el-table__header .has-gutter .cell {
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
    }

    .storeManage-dialog > > > .el-dialog .el-dialog__header {
      display: flex;
      align-items: center;
      height: 48px;
      background: #F7F8FA;
      padding: 0px 0px 0px 12px;
      .el-dialog__headerbtn {
        top: 15px;
      }
    }
    .look-dialog > > > .el-form-item--mini.el-form-item, .el-form-item--small.el-form-item {
      margin-bottom: 0px;
    }
    .storeManage-dialog {
      font-size: 14px;
      color: #606266;
      .input-with-select {
        > > > .el-input__inner {
          color: #333;
        }
      }
    }
    > > > .el-button {
      line-height: 1.2;
    }
  }
</style>
